package com.dingding.order.core.special.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.base.daox.IBaseDaoX;
import com.dingding.common.core.beans.special.Specialuserlogin;
import com.dingding.common.core.beans.special.Userspecialorder;
import com.dingding.common.core.beans.special.UserspecialorderT;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.common.core.util.StringUtils;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.amap.amapDto.GetDistanceDto;
import com.dingding.order.code.order.SpecialOrderStatus;
import com.dingding.order.core.special.dao.ISpecialOrderDao;
import com.dingding.order.core.special.sql.SpecialOrderSql;
import com.dingding.order.dto.march.underway.UderWayOrderDto;

@Component("specialOrderDao")
public class SpecialOrderDaoImpl extends BaseDaoImpl<UserspecialorderT> implements ISpecialOrderDao{

	Logger log =  Logger.getLogger(this.getClass());
	
	@Resource(name = "baseDaoX")
	private IBaseDaoX basedao;
	
	/**
	 * @Description:获取所有司机的经纬度信息
	 * @author fengshuonan
	 * @since 2.4
	 */
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public List<GetDistanceDto> getAllDriverCarInfos(String currentCity,Integer userRole) {
		List<GetDistanceDto> list = null;
		try {
			list = (List<GetDistanceDto>) super.listSqlFreeObject(
					SpecialOrderSql.GET_ALL_DRIVERS, GetDistanceDto.class,StringUtils.cityLike(currentCity),userRole);
		} catch (Exception e) {
			log.error("查询所有司机车辆信息出错！SpecialOrderDaoImpl ： getAllDriverCarInfos()",e);
		}
		if(list == null || list.size() == 0){
			return null;
		}else{
			return list;
		}
	}

	
	/**
	 * @Description:获取专车的订单状态
	 * @author fengshuonan
	 * @return int （0 查询不到订单）
	 * @since 2.4
	 */
	@Override
	public int getOrderStatus(String orderId) {
		try {
			UserspecialorderT userspecialordert = super.getById(orderId);
			if(userspecialordert == null){
				//从正式库中查询
				Userspecialorder zorder = this.basedao.getById(orderId, Userspecialorder.class);
				if(zorder != null && zorder.getOrderStatus() != null){
					return zorder.getOrderStatus();
				}else{
					return 0;
				}
			}else{
				return userspecialordert.getOrderStatus();
			}
		} catch (Exception e) {
			log.error("获取专车订单状态错误！SpecialOrderDaoImpl：getOrderStatus()",e);
			return 0;
		}
	}

	@Override
	@SuppressWarnings({ "unchecked", "deprecation" })
	public List<UderWayOrderDto> getSpecialUnderWayOrders(String userId,Integer clientType) {
		
		List<UderWayOrderDto> list = null;
		
		List<UderWayOrderDto> taxiList = null;
		
		try {
			
			// 获取专车进行中订单
			list = (List<UderWayOrderDto>) super.listSqlFreeObject(SpecialOrderSql.getUnderWayOrder(clientType), 
					UderWayOrderDto.class,userId);
			
			// 获取出租车进行中订单
			if(clientType == 1){//1:司机 客户端 2:乘客客户端
				taxiList = (List<UderWayOrderDto>) super.listSqlFreeObject(SpecialOrderSql.GET_DRIVER_UNDER_WAY_TAXI_ORDER, UderWayOrderDto.class,userId);
			}else{
				taxiList = (List<UderWayOrderDto>) super.listSqlFreeObject(SpecialOrderSql.GET_UNDER_WAY_TAXI_ORDER, UderWayOrderDto.class,userId);
			}
			
		} catch (Exception e) {
			log.error("获取专车进行中订单出错！",e);
		}
		
		if((list == null || list.size() == 0) && (taxiList == null || taxiList.size() == 0)){
			return new ArrayList<UderWayOrderDto>();
		}else{
			
			// 出租车
			for (UderWayOrderDto uderWayOrderDto : taxiList) {
				
				uderWayOrderDto.setPublishType(2); // 发单类型（1:顺风车 2:出租车 3:专车4:一口价）
				
				// 当前订单该用户角色
				if(clientType == 1){
					uderWayOrderDto.setUserType(1);
				}else if(clientType == 2){
					uderWayOrderDto.setUserType(2);
				}
				
				uderWayOrderDto.setUserAvatar(ToolUtil.imageAddress(uderWayOrderDto.getUserAvatar()));
				uderWayOrderDto.setUserTabloidAvatar(ToolUtil.imageAddress(uderWayOrderDto.getUserTabloidAvatar()));
				uderWayOrderDto.setOrderValidateTime(uderWayOrderDto.getCreatTime());//设置订单生效时间
				uderWayOrderDto.setOutTime(DateFormatUtil.getMMddEHHss(uderWayOrderDto.getSetOutTime()));//设置订单的出发时间（显示用的）
			}
			
			//设置订单类型为3：专车类型
			for(UderWayOrderDto dto : list){
				
				// 当前订单该用户角色
				if(clientType == 1){
					dto.setUserType(1);
				}else if(clientType == 2){
					dto.setUserType(2);
				}
				
				dto.setUserAvatar(ToolUtil.imageAddress(dto.getUserAvatar()));
				dto.setUserTabloidAvatar(ToolUtil.imageAddress(dto.getUserTabloidAvatar()));
				dto.setOrderValidateTime(dto.getCreatTime());//设置订单生效时间
				dto.setOutTime(DateFormatUtil.getMMddEHHss(dto.getSetOutTime()));//设置订单的出发时间（显示用的）
			}
			
			list.addAll(taxiList);
			
			return list;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void OrderTempToFormal(String ordertId) {
		
		UserspecialorderT userspecialorderT = super.getById(ordertId);
		Userspecialorder orderNew = new Userspecialorder();
		BeanUtils.copyProperties(userspecialorderT, orderNew);
		orderNew.setSpecialOrderId(userspecialorderT.getSpecialOrderId());
		orderNew.setOrderStatus(SpecialOrderStatus.ORDER_OK);
		orderNew.setIsComment(1);
		
		try {
			this.basedao.save(orderNew);
			this.basedao.delete(userspecialorderT);
		} catch (Exception e) {
			log.error("迁移订单出错！SpecialOrderDaoImpl：OrderTempToFormal()",e);
		}
	}


	@Override
	public Specialuserlogin getDriverLogin(String userId) {
		
		Specialuserlogin specialuserlogin = null;
		try {
			specialuserlogin = this.basedao.getById(userId, Specialuserlogin.class);
		} catch (Exception e) {
			log.error("获取司机详细信息出错！SpecialOrderDaoImpl：getDriverLogin()",e);
		}
		
		if(specialuserlogin != null){
			return specialuserlogin;
		}else{
			return null;
		}
	}

}
